package com.todo.user.application.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.todo.common.common.entity.response.page.PageResult;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.exception.BizException;
import com.todo.common.utils.TodoCommonUtils;
import com.todo.user.application.convert.UserServiceConvert;
import com.todo.user.application.entity.bo.FollowFriendBO;
import com.todo.user.application.entity.bo.FollowFriendInfoBO;
import com.todo.user.application.service.UserRelationService;
import com.todo.user.common.enums.FriendTypeEnum;
import com.todo.user.infrastructure.entity.condition.UserRelationCondition;
import com.todo.user.infrastructure.entity.po.UserRelationPO;
import com.todo.user.infrastructure.repository.UserRelationRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * (UserRelation)表服务实现类
 *
 * @author zhangwenxiang
 * @since 2024-03-04 00:19:50
 */
@Slf4j
@Service("userRelationService")
public class UserRelationServiceImpl implements UserRelationService {

    @Resource
    private UserRelationRepository userRelationRepository;


    @Override
    public FollowFriendInfoBO queryOtherUserInfoDetail(Long currentUserId, Long otherUserId) {
        if (currentUserId == null || otherUserId == null || currentUserId < 0 || otherUserId < 0) {
            return null;
        }
        UserRelationPO userRelationPO = userRelationRepository.queryUserRelationByUserId(currentUserId, otherUserId);
        return UserServiceConvert.convertToFollowFriendInfoBO(userRelationPO);
    }

    @Override
    public Boolean followFriend(FollowFriendInfoBO followFriendInfoBO) {
        if (followFriendInfoBO == null || followFriendInfoBO.getUserId() == null || followFriendInfoBO.getRelationUserId() == null) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }

        Long userId = followFriendInfoBO.getUserId();
        Long relationUserId = followFriendInfoBO.getRelationUserId();
        FriendTypeEnum friendType = followFriendInfoBO.getFriendType();
        if (FriendTypeEnum.FOLLOW.equals(friendType) || FriendTypeEnum.SHIELD.equals(friendType) || FriendTypeEnum.BLACKLIST.equals(friendType)) {
            //查询历史关联，为空则新增
            UserRelationPO alreadyUserRelationPO = userRelationRepository.queryUserRelationByUserId(userId, relationUserId);
            if (alreadyUserRelationPO == null) {
                UserRelationPO userRelationPO = new UserRelationPO();
                userRelationPO.setId(TodoCommonUtils.buildLongGuid());
                userRelationPO.setUserId(userId);
                userRelationPO.setRelationUserId(relationUserId);
                userRelationPO.setRelationType(friendType.getType());
                userRelationPO.setIsDelete(0);
                userRelationPO.setCreateTime(new Date());
                userRelationPO.setUpdateTime(new Date());
                Boolean relationResult = userRelationRepository.insertUserRelationPO(userRelationPO);
                return relationResult;
            }
            if (alreadyUserRelationPO.getRelationType().equals(friendType.getType())) {
                log.info("UserInfoService#followFriend 历史关联关系相同，无需操作 followFriendInfo：【{}】，alreadyUserRelationPO【{}】", JSONObject.toJSONString(followFriendInfoBO), JSONObject.toJSONString(alreadyUserRelationPO));
                return true;
            }
            alreadyUserRelationPO.setRelationType(friendType.getType());
            return userRelationRepository.updateUserRelationPOById(alreadyUserRelationPO);
        }
        //取消关联关系
        if (FriendTypeEnum.NONE.equals(friendType)) {
            UserRelationPO userRelationPO = userRelationRepository.queryUserRelationByUserId(userId, relationUserId);
            if (userRelationPO == null) {
                //不存在关联默认返回成功
                return true;
            }
            //存在关联时候直接删除数据
            return userRelationRepository.removeUserRelationPOById(userRelationPO.getId());
        }
        return true;
    }

    @Override
    public PageResult<FollowFriendInfoBO> followFriendsList(FollowFriendBO followFriendBO) {
        UserRelationCondition userRelationCondition = new UserRelationCondition();
        userRelationCondition.setUserId(followFriendBO.getUserId());
        userRelationCondition.setRelationUserId(followFriendBO.getFriendId());
        userRelationCondition.setRelationType(followFriendBO.getRelationType().getType());
        userRelationCondition.setCurrentIndex(followFriendBO.getCurrentIndex());
        userRelationCondition.setPageSize(followFriendBO.getPageSize());
        userRelationCondition.setPageIndex(followFriendBO.getPageIndex());
        PageResult<UserRelationPO> userRelationPOPageResult = userRelationRepository.queryUserRelationPOByCondition(userRelationCondition);
        if (userRelationPOPageResult == null || CollectionUtils.isEmpty(userRelationPOPageResult.getResults())) {
            log.warn("UserInfoService#followFriendsList 查询用户关联列表完成 数据为空 followFriendBO：【{}】", JSONObject.toJSONString(followFriendBO));
            return PageResult.of(Lists.newArrayList(), followFriendBO.getPageIndex(), followFriendBO.getPageSize(), 0);
        }
        PageResult<FollowFriendInfoBO> result = UserServiceConvert.convertToPageFollowFriendInfoBO(userRelationPOPageResult);
        return result;
    }

}
